using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using MishraReader.AppServices;
using MishraReader.Entities;
using MishraReader.ViewModels.Base;

namespace MishraReader.ViewModels.Reader
{
    public interface IBookmarkViewModel : IDialogViewModel
    {
        /// <summary>
        ///   Gets the item.
        /// </summary>
        Item Item { get; set; }
    }

    public class BookmarkViewModel : ShareViewModelBase, IBookmarkViewModel
    {
        private readonly IOptionsManager _optionsManager;
        private readonly IShareServiceManager _shareManager;


        public BookmarkViewModel(Item item,
                                 IOptionsManager optionsManager,
                                 IShareServiceManager shareManager,
                                 ILightWeakEventManager eventManager,
                                 Func<IShareServicePackage, bool, ShareServiceViewModel> shareVmFactory)
            : base(item, ServiceType.Bookmark, optionsManager, eventManager, shareVmFactory)
        {
            _optionsManager = optionsManager;
            _shareManager = shareManager;
            BookmarkCommand = new RelayCommand<ShareServiceViewModel>(ExecuteBookmarkCommand);
        }

        public ICommand BookmarkCommand { get; private set; }

        /// <summary>
        ///   Executes the bookmark command.
        /// </summary>
        private void ExecuteBookmarkCommand(ShareServiceViewModel serviceViewModel)
        {
            if (serviceViewModel == null)
                return;

            var account = _optionsManager.ActivatedServices.FirstOrDefault(s => s.ServicePackage == serviceViewModel.ServicePackage);
            if (account != null)
                _shareManager.AddShareJob(account, Item.Title, Item);

            ExecuteOkayCommand();
        }
    }
}